#include "splash.h"

#include <QtWidgets/QtWidgets>
#include <array>

#include "tracer.h"
#include "config-fnt.h"

struct SplashPriv {
	Splash* splash;


	SplashPriv(Splash* s):
		splash{s}
	{
		auto vlayout=new QVBoxLayout{splash};

		auto hlayout=new QHBoxLayout{};
		vlayout->addLayout(hlayout, 0);

		auto vlayouticon=new QVBoxLayout{};
		hlayout->addLayout(vlayouticon, 0);

		auto iconBtn=new QLabel{splash};
		vlayouticon->addWidget(iconBtn, 0);
		vlayouticon->addStretch(1);
		iconBtn->setPixmap(QPixmap{":/images/generated/icon.svg"});
		iconBtn->setScaledContents(true);
		iconBtn->setMinimumSize(96, 96);
		iconBtn->setMaximumSize(96, 96);

		auto vlayout2=new QVBoxLayout{};
		hlayout->addLayout(vlayout2, 1);
		auto label=new QLabel(PACKAGE_NAME_LONG, splash, 0);
		vlayout2->addWidget(label, 0);
		auto font=label->font();
		font.setPixelSize(36);
		font.setBold(true);
		label->setFont(font);

		auto hlayout2=new QHBoxLayout{};
		vlayout2->addLayout(hlayout2, 1);
		auto vlayoutver=new QVBoxLayout{};
		hlayout2->addLayout(vlayoutver, 0);
		label=new QLabel(PACKAGE_VERSION, splash, 0);
		vlayoutver->addWidget(label, 0);
		//font=label->font();
		//font.setPixelSize(24);
		//label->setFont(font);
		vlayoutver->addStretch(1);
		hlayout2->addStretch(1);
		auto vlayouthelp=new QVBoxLayout{};
		hlayout2->addLayout(vlayouthelp, 0);
		vlayouthelp->addStretch(1);

		auto& actions=Tracer::instance()->tracerActions();
		auto hlayouthelp=new QHBoxLayout{};
		vlayouthelp->addLayout(hlayouthelp, 0);
		hlayouthelp->addStretch(1);
		auto btn=new QPushButton{actions[TRACER_ABOUT]->icon(), actions[TRACER_ABOUT]->text(), splash};
		hlayouthelp->addWidget(btn, 0);
		btn->setToolTip(actions[TRACER_ABOUT]->toolTip());
		QObject::connect(btn, &QPushButton::clicked, actions[TRACER_ABOUT], &QAction::trigger);
		btn=new QPushButton{actions[TRACER_MANUAL]->icon(), actions[TRACER_MANUAL]->text(), splash};
		hlayouthelp->addWidget(btn, 0);
		btn->setToolTip(actions[TRACER_MANUAL]->toolTip());
		QObject::connect(btn, &QPushButton::clicked, actions[TRACER_MANUAL], &QAction::trigger);

		auto line=new QFrame{splash};
		vlayout->addWidget(line, 0);
		line->setFrameShape(QFrame::HLine);

		auto hlayout3=new QHBoxLayout{};
		vlayout->addLayout(hlayout3, 0);

		btn=new QPushButton{"Recent", splash};
		hlayout3->addWidget(btn, 0);
		btn->setMenu(Tracer::instance()->recentMenu());

		btn=new QPushButton{"Recover", splash};
		hlayout3->addWidget(btn, 0);
		btn->setMenu(Tracer::instance()->recoverMenu());

		hlayout3->addStretch(1);

		btn=new QPushButton{actions[SESSION_NEW]->icon(), actions[SESSION_NEW]->text(), splash};
		hlayout3->addWidget(btn, 0);
		btn->setToolTip(actions[SESSION_NEW]->toolTip());
		QObject::connect(btn, &QPushButton::clicked, actions[SESSION_NEW], &QAction::trigger);

		btn=new QPushButton{actions[SESSION_OPEN]->icon(), actions[SESSION_OPEN]->text(), splash};
		hlayout3->addWidget(btn, 0);
		btn->setToolTip(actions[SESSION_OPEN]->toolTip());
		btn->setDefault(true);
		//btn->setMenu(Tracer::instance()->recentMenu());
		QObject::connect(btn, &QPushButton::clicked, actions[SESSION_OPEN], &QAction::trigger);

	}
	~SplashPriv() {
	}
};

Splash* Splash::_instance{nullptr};
void Splash::closeEvent(QCloseEvent* event) {
	Tracer::instance()->tracerActions()[TRACER_QUIT]->trigger();
}

Splash::Splash():
	QDialog{nullptr, 0}, priv{new SplashPriv{this}}
{
	layout()->setSizeConstraint(QLayout::SetFixedSize);
}

Splash::~Splash() {
	delete priv;
}
